﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RM2012Server.Model;
using System.Data;
using RM2012Server.Business;
using DevComponents.DotNetBar.Controls;
using System.Windows.Forms;

namespace RM2012Server.Controller
{
    public class SupplierCtrl
    {
        SupplierDAO m_SupplierDAO = new SupplierDAO();

        public void LoadToComboBox(ComboBoxEx cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "SupplierName";
            cmb.ValueMember = "SupplierId";
        }

        public void LoadToDataGridViewComboBoxColumn(DataGridViewComboBoxColumn cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "SupplierName";
            cmb.ValueMember = "SupplierId";
            cmb.DataPropertyName = "SupplierId";
            cmb.HeaderText = "Nhà cung cấp";
        }

        public void LoadToDataGridViewComboBoxExColumn(DataGridViewComboBoxExColumn cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "SupplierName";
            cmb.ValueMember = "SupplierId";
            cmb.DataPropertyName = "SupplierId";
            cmb.HeaderText = "Nhà cung cấp";
        }

        public void Show(DataGridViewX dgv, BindingNavigator bn)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = GetAll();
            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public void Show(DataGridViewX dgv,
                         BindingNavigator bn,
                         TextBoxX txtSupplierId,
                         TextBoxX txtSupplierName,
                         TextBoxX txtTaxCode,
                         TextBoxX txtState,
                         TextBoxX txtAddress,
                         TextBoxX txtPhone,
                         TextBoxX txtEmail,
                         TextBoxX txtWebsite,
                         TextBoxX txtRemark)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = GetAll();

            txtSupplierId.DataBindings.Clear();
            txtSupplierId.DataBindings.Add("Text", bs, "SupplierId");

            txtSupplierName.DataBindings.Clear();
            txtSupplierName.DataBindings.Add("Text", bs, "SupplierName");

            txtTaxCode.DataBindings.Clear();
            txtTaxCode.DataBindings.Add("Text", bs, "TaxCode");

            txtState.DataBindings.Clear();
            txtState.DataBindings.Add("Text", bs, "State");

            txtAddress.DataBindings.Clear();
            txtAddress.DataBindings.Add("Text", bs, "Address");

            txtPhone.DataBindings.Clear();
            txtPhone.DataBindings.Add("Text", bs, "Phone");

            txtEmail.DataBindings.Clear();
            txtEmail.DataBindings.Add("Text", bs, "Email");

            txtWebsite.DataBindings.Clear();
            txtWebsite.DataBindings.Add("Text", bs, "Website");

            txtRemark.DataBindings.Clear();
            txtRemark.DataBindings.Add("Text", bs, "Remark");

            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }


        public void ShowWithSources(DataGridViewX dgv,
                         BindingNavigator bn,
                         TextBoxX txtSupplierId,
                         TextBoxX txtSupplierName,
                         TextBoxX txtTaxCode,
                         TextBoxX txtState,
                         TextBoxX txtAddress,
                         TextBoxX txtPhone,
                         TextBoxX txtEmail,
                         TextBoxX txtWebsite,
                         TextBoxX txtRemark,
                         DataTable dtSources)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = dtSources;

            txtSupplierId.DataBindings.Clear();
            txtSupplierId.DataBindings.Add("Text", bs, "SupplierId");

            txtSupplierName.DataBindings.Clear();
            txtSupplierName.DataBindings.Add("Text", bs, "SupplierName");

            txtTaxCode.DataBindings.Clear();
            txtTaxCode.DataBindings.Add("Text", bs, "TaxCode");

            txtState.DataBindings.Clear();
            txtState.DataBindings.Add("Text", bs, "State");

            txtAddress.DataBindings.Clear();
            txtAddress.DataBindings.Add("Text", bs, "Address");

            txtPhone.DataBindings.Clear();
            txtPhone.DataBindings.Add("Text", bs, "Phone");

            txtEmail.DataBindings.Clear();
            txtEmail.DataBindings.Add("Text", bs, "Email");

            txtWebsite.DataBindings.Clear();
            txtWebsite.DataBindings.Add("Text", bs, "Website");

            txtRemark.DataBindings.Clear();
            txtRemark.DataBindings.Add("Text", bs, "Remark");

            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public DataTable GetAll()
        {
            return m_SupplierDAO.GetAll();
        }

        public DataTable GetById(String supplierId)
        {
            return m_SupplierDAO.GetById(supplierId);
        }

        public DataTable GetByLikeId(String supplierId)
        {
            return m_SupplierDAO.GetByLikeId(supplierId);
        }

        public DataTable GetByName(String supplierName)
        {
            return m_SupplierDAO.GetByName(supplierName);
        }

        public DataTable GetByTaxCode(String taxCode)
        {
            return m_SupplierDAO.GetByTaxCode(taxCode);
        }

        public DataTable GetByAddress(String address)
        {
            return m_SupplierDAO.GetByAddress(address);
        }

        public DataTable GetByPhone(String phone)
        {
            return m_SupplierDAO.GetByPhone(phone);
        }

        public DataTable GetByEmail(String email)
        {
            return m_SupplierDAO.GetByEmail(email);
        }

        public DataTable GetByWebsite(String website)
        {
            return m_SupplierDAO.GetByWebsite(website);
        }

        public int Add(SupplierInfo info)
        {
            return m_SupplierDAO.Add(info);
        }

        public int Update(SupplierInfo info)
        {
            return m_SupplierDAO.Update(info);
        }

        public int Delete(String supplierId)
        {
            return m_SupplierDAO.Delete(supplierId);
        }

        public DataRow AddNewRow()
        {
            return m_SupplierDAO.AddNewRow();
        }

        public void AddNewSupplier(DataRow m_Row)
        {
            m_SupplierDAO.AddNewSupplier(m_Row);
        }

        public bool SaveData()
        {
            return m_SupplierDAO.SaveData();
        }
    }
}
